﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;


namespace sales.Controls
{
    //Incomplete
    public class gridOrderDetailAttach : common.controls.baseDataGridView 
    {
        public gridOrderDetailAttach()
        {
            this.AutoArrange();
            this.myOnFind += new OnCellEvent(FindData);
        }

        public enum ColumnNames
        {
            deleted,  itemCode, itemName, commonName, unit, qty, notes
        };

        public void UpdateItemList()
        {
            UpdateItemList(null);
        }
        public void UpdateItemList(string itemCode)
        {
            if (itemCode == null)
            {
                for (int idx = 0; idx < this.Rows.Count; idx++)
                {
                    if (this.Rows[idx].Cells[ColumnNames.itemCode.ToString()].Value == null) continue;
                    DataAccess.DataCache.FindITEM(this.Rows[idx].Cells[ColumnNames.itemCode.ToString()].Value.ToString());
                }
            }
            else DataAccess.DataCache.FindITEM(itemCode);
        }
        public override void Init(BindingSource dataSource)
        {
            base.Init(dataSource);

            data.salesDS.OrderDetailAttachDataTable detailTbl = (data.salesDS.OrderDetailAttachDataTable)dataSource.DataSource;
            DataGridViewCellStyle qtyCellStyle = new DataGridViewCellStyle();

            qtyCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            qtyCellStyle.Format = "N" + common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskQtySales).ToString(); qtyCellStyle.NullValue = null;

            //delete
            DataGridViewCheckBoxColumn deleted = new DataGridViewCheckBoxColumn();
            deleted.DataPropertyName = detailTbl.deletedColumn.ColumnName;
            deleted.Name = ColumnNames.deleted.ToString();
            deleted.Width = 20;
            deleted.HeaderText = "";
            deleted.Visible = false;

            // 
            // itemCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemCode.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            itemCode.Uppercase = true;
            itemCode.HeaderText = "Mã.hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 100;
            itemCode.Uppercase = true;

            // 
            // itemName
            // 
            BindingSource itemSource = new BindingSource();
            itemSource.DataSource = DataAccess.DataCache.myMasterDS.Item;

            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            itemName.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            itemName.DataSource = itemSource;
            itemName.DisplayMember = DataAccess.DataCache.myMasterDS.Item.nameColumn.ColumnName;
            itemName.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemName.HeaderText = "Diễn giải";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;
            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemName.Width = 200;
            itemName.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemName.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;

            // 
            // commonName
            // 
            DataGridViewComboBoxColumn commonName = new DataGridViewComboBoxColumn();
            commonName.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            commonName.DataSource = itemSource;
            commonName.DisplayMember = DataAccess.DataCache.myMasterDS.Item.commonNameColumn.ColumnName;
            commonName.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            commonName.HeaderText = "Tên thường gọi";
            commonName.Name = ColumnNames.commonName.ToString();
            commonName.ReadOnly = true;
            commonName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            commonName.Width = 300;
            commonName.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            commonName.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;
            // 
            // unit
            // 
            DataGridViewComboBoxColumn unit = new DataGridViewComboBoxColumn();
            unit.DataPropertyName = detailTbl.itemCodeColumn.ColumnName;
            unit.DataSource = itemSource;
            unit.DisplayMember = DataAccess.DataCache.myMasterDS.Item.unitColumn.ColumnName;
            unit.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            unit.HeaderText = "ĐVT";
            unit.Name = ColumnNames.unit.ToString();
            unit.ReadOnly = true;
            unit.ValueMember = "itemCode";
            unit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            unit.Width = 50;
            unit.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            unit.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;

            // 
            // Qty
            // 
            DataGridViewTextBoxColumn qty = new DataGridViewTextBoxColumn();
            qty.DataPropertyName = detailTbl.qtyColumn.ColumnName;
            qty.DefaultCellStyle = qtyCellStyle;
            qty.HeaderText = "Lượng";
            qty.Name = ColumnNames.qty.ToString();
            qty.Width = 70;

            // 
            // Notes
            // 
            DataGridViewTextBoxColumn notes = new DataGridViewTextBoxColumn();
            notes.DataPropertyName = detailTbl.notesColumn.ColumnName;
            notes.HeaderText = "Ghi chú";
            notes.Name = ColumnNames.notes.ToString();
            notes.Width = 100;
            notes.Visible = false;

            this.DataSource = dataSource;
            this.Columns.Clear();
            this.Columns.AddRange(new DataGridViewColumn[] {
                    deleted,itemCode,itemName,commonName, qty,unit,notes});
            this.ScrollBars = ScrollBars.Vertical;

            this.myAutoFitColumn = itemName.Name;
            this.DisbaleReadOnlyColumns();
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
        }
        
        public override void Refresh()
        {
            if (IsOnProcessing) return;
            try
            {
                IsOnProcessing = true;
                UpdateItemList(null);
                IsOnProcessing = false;
                base.Refresh();
            }
            catch (Exception er)
            {
                IsOnProcessing = false;
                ErrorHandler(this, er);
            }
        }

        public override bool CheckData()
        {
            this.myDataSource.EndEdit();
            return DoValidateData(OnRowValidate);
        }
        private void OnRowValidate(DataGridViewRow row, common.controls.GridRowValidateEventArgs e)
        {
            if (row.DataBoundItem == null) return;

            if (this.Columns[ColumnNames.itemCode.ToString()].Visible)
            {
                if (row.Cells[ColumnNames.itemCode.ToString()].Value.ToString().Trim() == "")
                {
                    e.ErrorText.Add(String.Format("Dữ liệu [{0}] trống", this.Columns[ColumnNames.itemCode.ToString()].HeaderText));
                    e.HaveError = true;
                }
            }
            //=========================
            //Check data contrainsts
            //=========================

            if (this.Columns[ColumnNames.itemCode.ToString()].Visible &&
                row.Cells[ColumnNames.itemCode.ToString()].Value.ToString().Trim() != "" &&
                DataAccess.DataCache.FindITEM(row.Cells[ColumnNames.itemCode.ToString()].Value.ToString()) == null)
            {
                e.ErrorText.Add(String.Format("Dữ liệu [{0}] không hợp lệ", this.Columns[ColumnNames.itemCode.ToString()].HeaderText));
                e.HaveError = true;
            }
        }

        
        private void FindData(object sender, common.controls.GridEventArgs arg)
        {
            string findCode = this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].EditedFormattedValue.ToString(); ;
            if (this.CurrentCell == null) return;
            //Code
            if (this.Columns[arg.ColumnIndex].Name.Trim() == ColumnNames.itemCode.ToString())
            {
                application.dataFind.myItemFind.Find(findCode, true);
                data.masterDS.ItemRow itemRow = application.dataFind.myItemFind.selectedDataRow;
                if (itemRow != null)
                {
                    this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].Value = itemRow.itemCode;
                    UpdateItemList(itemRow.itemCode);
                }
                else this.Rows[arg.RowIndex].Cells[arg.ColumnIndex].Value = DBNull.Value;
                this.myDataSource.ResetCurrentItem();
                this.myDataSource.EndEdit(); 
            }
        }
    }
}
